<template>
  <el-row class="home__to-do-task">
    <el-table
      v-loading="loading"
      :data="taskList"
      class="home__to-do-task__table"
    >
      <el-table-column type="index" label="序号" width="50" align="center"> </el-table-column>
      <el-table-column prop="name" label="任务名称" min-width="180" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="pageTo(scope.row)">{{
            scope.row.name
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="任务类型" min-width="180" align="center">
      </el-table-column>
      <el-table-column prop="count" label="所需处理" min-width="180" align="center">
        <template slot-scope="scope">
          {{ (scope.row.count || 1) + " 条" }}
        </template>
      </el-table-column>
      <el-table-column prop="time" label="获取时间" min-width="180" align="center">
      </el-table-column>
      <!-- <el-table-column prop="perpeo" label="发起人" min-width="180">
      </el-table-column> -->
    </el-table>
  </el-row>
</template>

<script>
import { listWorkload } from "@/api/md/workload";
import { WORKLOAD_TYPE } from "@/views/mes/md/workload/data";
import { TASK_PATH } from "@/utils/const.js";
import { formatDate } from "@/utils/index.js"
export default {
  name: "ToDoTask",
  data() {
    return {
      loading: false,
      getWorkParams: {},
      workloadList: [],
      pageInfo: {
        pageNum: 10,
        pageSize: 10,
      },
      taskList: [],
    };
  },
  computed: {
    isCheckUser() {
      const roles = this.$store.getters.roles;
      return roles.includes("work_load_audit") || roles.includes("admin");
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.getTaskList();
    },
    async getTaskList() {
      const formData = {
        userId: "",
        businessKey: "",
        ...this.pageInfo,
      };
      try {
        // const res = await getTaskListAsync(this.formData);
        const res = await this.getTaskListAsync(this.formData);
        this.loading = false;

        if (res.code === 200) {
          this.taskList = res.data;
        }
      } catch (e) {
        console.error(e);
      }
    },
    pageTo(task) {
      this.$router.push({
        path: task.path,
        query: task.query,
      });
    },
    getWorkTask() {
      /** 查询工作量列表 */
      this.loading = true;
      if (this.isCheckUser) {
        // 审核角色
        const params = {
          wlStatus: WORKLOAD_TYPE.checkPadding,
        };
        listWorkload(params).then((response) => {
          if (response.total > 0) {
            this.taskList.push({
              name: `审查工作量`,
              taskNum: response.total,
              type: "日常任务",
              time: this.parseTime(new Date()),
              count: response.total,
              path: TASK_PATH.WORKLOAD,
              query: { wlStatus: WORKLOAD_TYPE.checkPadding },
            });
          }
        });
      }
      // 普通角色 - 修改
      const params = {
        wlHandler: this.$store.getters.nickName,
        wlStatus: WORKLOAD_TYPE.update,
      };
      listWorkload(params).then((response) => {
        if (response.total > 0) {
          this.taskList.push({
            name: `修改工作量`,
            taskNum: response.total,
            type: "日常任务",
            time: this.parseTime(new Date()),
            count: response.total,
            path: TASK_PATH.WORKLOAD,
            query: { wlStatus: WORKLOAD_TYPE.update },
          });
        }
      });

      // 普通角色 - 填写
      const params2 = {
        wlHandler: this.$store.getters.nickName,
        wlTimestamp: formatDate(new Date(), "yyyy-mm-dd hh:mi:ss") 
      };
      listWorkload(params2).then((response) => {
        console.log(222, params2, response)
        if (response.total == 0) {
          this.taskList.push({
            name: `填写工作量`,
            taskNum: response.total,
            type: "日常任务",
            time: this.parseTime(new Date()),
            count: response.total,
            path: TASK_PATH.WORKLOAD,
            // query: { wlStatus: WORKLOAD_TYPE.update },
          });
        }
      });
      // listWorkload(this.getWorkParams).then((response) => {
      //   this.workloadList = response.rows;
      //   this.total = response.total;
      // });
    },
    // 临时函数
    async getTaskListAsync() {
      const workloadList = await this.getWorkTask();
      console.log(1, workloadList)
      return {
        code: 200,
        data: [],
      };
    },
  },
};
</script>

<style lang="scss" scoped>
.home__to-do-task {
  margin-bottom: 32px;
  border-radius: 10px;
  border-color: rgba(0, 0, 0, 0.05);
  padding: 10px;
  box-shadow: 4px 4px 40px rgba(0, 0, 0, 0.05);
  background: #fff;
  color: #666;
  &__table {
    width: 100%;
  }
}
</style>
